<?php

namespace app\award;

use app\controllers\h5\uni\HomeController;
use app\models\AdminSupplier;
use app\models\AwardList;
use app\models\AwardResult;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\user\UserScoreTicket;
use app\models\UserBalance;
use app\models\UserStore;
use app\models\UserStoreLog;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award
{
    protected $user;
    protected $order;
    protected $set;

    protected $items;

    // 总金额
    protected $amount;

    protected $is_bao = false;
    protected $is_buy = false;

    // 是否为重算订单
    protected $is_reset = false;


    public function __construct(Order $order, $is_reset = false)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $order->user_id]);
        $this->set = SetUserUp::getLastOne();

        $this->is_reset = $is_reset;

        $this->items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 复购商品价格
        $this->price = Product::find()
            ->select(['price'])
            ->where(['id' => $product_id])
            ->scalar();

        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 身份
        $this->is_bao = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT']);
        if ($this->is_bao) {
            $this->recommend();
        }

        $this->is_buy = ProductAreaList::checkInArea($product_id, Product::$areaMap['BUY']);
        if ($this->is_buy) {
            $this->recommend();
        }

    }

    private function recommend()
    {
        $r = Chain::getReferrer($this->order->user_id, true);
        if ($r) {
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->vip2_1 / 100 * $this->amount;
                $this->saveCommission($r->id, $commission, 1, '直推');
            }
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->vip1_1 / 100 * $this->amount;
                $this->saveCommission($r->id, $commission, 1, '直推');
            }

            $r2 = Chain::getReferrer($r->id, true);
            if ($r2) {
                if ($r2->level == UserLevel::$keys['vip2']) {
                    $commission = $this->set->vip2_2 / 100 * $this->amount;
                    $this->saveCommission($r->id, $commission, 2, '间推');
                }
                if ($r2->level == UserLevel::$keys['vip1']) {
                    $commission = $this->set->vip1_2 / 100 * $this->amount;
                    $this->saveCommission($r2->id, $commission, 2, '间推');
                }

                $t = Chain::getReferrerChain($r2->id, [UserLevel::$keys['vip2']], true);
                if ($t) {
                    $t = $t[0];
                    $commission = $this->set->team / 100 * $this->amount;
                    $this->saveCommission($t->id, $commission, 3, '团队奖');
                }
            }
        }
    }


    // 保存佣金 (await balance)
    protected function saveCommission($user_id, $balance, $type, $name, $status = 1)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = $status;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();
            $account = UserAccount::findOne(['user_id' => $user_id]);

            if ($account) {
                if ($status == 1) {
                    $account->balance += $balance;
                } else {
                    $account->await_balance += $balance;
                }

              $account->save();
            }

            // balance logs
            if ($status == 1) {
                UserBalance::addLog($model->user_id,
                    $account->balance, $model->balance, $account->balance + $model->balance, '返佣', $this->order->id);
            }

            return true;
        }
    }

}